// pages/server_end/message/message.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    message01: [],
    message02: [],
    message03: [],
    message04: [],
    message05: [],
    token: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
          that.getMessage(0);
          that.getMessage(2);
          that.getMessage(1);
          that.getMessage(3);
          that.getMessage(7);
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getMessage(type) {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token,
      type: type
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getlastmsg, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        if (type == 0) {
          that.setData({
            message01: res.data.data
          })
        } else if (type == 1) {
          that.setData({
            message02: res.data.data
          })
        } else if (type == 2) {
          that.setData({
            message03: res.data.data
          })
        } else if (type == 2) {
          that.setData({
            message04: res.data.data
          })
        }else if (type == 7) {
          that.setData({
            message05: res.data.data
          })
        }
      }
    })
  },
  goMessage(e) {
    var type = e.currentTarget.dataset.type;
    if (this.data.token) {
      wx.navigateTo({
        url: '../message_list/message_list?type=' + type
      })
    } else {
      wx.navigateTo({
        url: '../../authorization/authorization'
      })
    }
  }
})